package com.dlc.shop.api.controller;


import com.alipay.api.AlipayApiException;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.dlc.shop.bean.app.param.PayParam;
import com.dlc.shop.bean.enums.PayEntry;
import com.dlc.shop.bean.pay.PayInfoDto;
import com.dlc.shop.common.bean.PaySettlementConfig;
import com.dlc.shop.common.config.Constant;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.manager.impl.PayManager;
import com.dlc.shop.security.api.util.SecurityUtils;
import com.dlc.shop.security.common.bo.LoginInfoBO;
import com.dlc.shop.security.common.manager.TokenStore;
import com.dlc.shop.security.common.util.AuthUserContext;
import com.dlc.shop.service.PayInfoService;
import com.dlc.shop.service.SysConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;

/**
 * @author LGH
 */
@RestController
@RequestMapping("/p/order")
@Tag(name = "订单支付接口")
@AllArgsConstructor
public class PayController {

    private final PayInfoService payInfoService;
    private final PayManager payManager;
    private final SysConfigService sysConfigService;
    private final TokenStore tokenStore;

    @PostMapping("/pay")
    @Operation(summary = "根据订单号进行支付" , description = "根据订单号进行支付")
    @SneakyThrows({WxPayException.class, AlipayApiException.class})
    public ServerResponseEntity<?> pay(HttpServletResponse httpResponse, @Valid @RequestBody PayParam payParam) {
        String userId = AuthUserContext.getUserId();
        String bizUserId = null;
        LoginInfoBO loginInfo = tokenStore.getLoginInfoByCache();
        if (loginInfo!=null) {
            bizUserId = loginInfo.getBizUserId();
        }
        PaySettlementConfig paySettlementConfig = sysConfigService.getSysConfigObject(Constant.PAY_SETTLEMENT_CONFIG, PaySettlementConfig.class);
        PayInfoDto payInfo = payInfoService.pay(userId, payParam,paySettlementConfig, bizUserId);
        payInfo.setBizUserId(bizUserId);
        payInfo.setUserId(userId);
        payInfo.setPayType(payParam.getPayType());
        payInfo.setApiNoticeUrl("/notice/pay/" + paySettlementConfig.getPaySettlementType() + "/" + PayEntry.ORDER.value() + "/" + payParam.getPayType());
        payInfo.setReturnUrl(payParam.getReturnUrl());
        payInfo.setSystemType(payParam.getAllinPaySystemType());
        return payManager.doPay(httpResponse, payInfo);
    }

    @GetMapping("/isPay/{payEntry}/{orderNumbers}")
    @Operation(summary = "根据订单号查询该订单是否已经支付" , description = "根据订单号查询该订单是否已经支付")
    @Parameter(name = "orderNumbers", description = "多个订单号拼接" , required = true)
    public ServerResponseEntity<Boolean> isPay(@PathVariable Integer payEntry, @PathVariable String orderNumbers) {
        String userId = SecurityUtils.getUser().getUserId();
        Integer count = payInfoService.queryPay(orderNumbers, userId, payEntry);
        boolean res = count != null && count == 1;
        return ServerResponseEntity.success(res);
    }
}
